package com.cp.hystrix;

import com.netflix.hystrix.HystrixCommand;
import org.springframework.web.client.RestTemplate;

/**
 * @author feipeng
 * @site www.gcp168.cn
 * @create 2020-08-23 17:31
 */
public class HelloCommand extends HystrixCommand<String> {

    RestTemplate restTemplate;
    String name;

    public HelloCommand(Setter setter,RestTemplate restTemplate,String name){
        super(setter);
        this.name = name;
        this.restTemplate = restTemplate;
    }

    @Override
    protected String run() throws Exception{
        //int i = 1/ 0;
        //return restTemplate.getForObject("http://provider/hello",String.class);
        return restTemplate.getForObject("http://provider/hello2?name={1}",String.class,name);
    }

    @Override
    protected String getCacheKey(){
        return name;
    }

    /**
     * 请求失败的回调
     * @return
     */
    @Override
    protected String getFallback(){
        return "error-extends:" + getExecutionException().getMessage();
    }




}
